﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using com.dz.ct.components;
using com.dz.ct.db;
using com.dz.ct.common;
using System.Threading;
using System.Text.RegularExpressions;
using com.dz.ct.components.SmallSite.www.google.com;
using System.Web;
using com.dz.ct.html;

namespace com.dz.ct.business
{


    public class BookCollectManager
    {
        #region 属性变量
        private DataSet m_baidu_ds = null;

        //文件读写
        FileManager m_file_mgr = new FileManager();
        /// <summary>
        /// 线程数量
        /// </summary>
        public int ThreadCount { get; set; }

        /// <summary>
        /// 盗版站点ID
        /// </summary>
        public int SiteId { get; set; }

        /// <summary>
        /// 需要收录章节的书籍
        /// </summary>
        private DataSet m_ds = null;

        /// <summary>
        /// 需要收录章节的书籍记录
        /// </summary>
        private int m_total_count = 0;

        /// <summary>
        /// 索引
        /// </summary>
        private int m_reg_index = 0;

        /// <summary>
        /// 每个线程的收录量
        /// </summary>
        private int m_per_thread_count = 0;

        /// <summary>
        /// 数据访问对象
        /// </summary>
        private DBManager m_db_mgr = new DBManager();

        /// <summary>
        /// 需要收录书籍的SQL
        /// </summary>
        public string BookSQL { get; set; }

        /// <summary>
        /// 接口对象
        /// </summary>
        IBigSiteObject t_obj = null;


        /// <summary>
        /// 触发发送消息的事件
        /// </summary>
        public event SendMsgEventHandler SendMsg;

        #region 收集书籍属性

        /// <summary>
        /// 书籍列表地址
        /// </summary>
        public string BookUrl { get; set; }

        /// <summary>
        /// 开始页码
        /// </summary>
        public int StartPageIndex { get; set; }

        /// <summary>
        /// 接口变量
        /// </summary>
        public string ComName { get; set; }

        #endregion

        /// <summary>
        /// 站点配置
        /// </summary>
        public DataRow SiteConfig { get; set; }

        /// <summary>
        /// 结束页码
        /// </summary>
        public int EndPageIndex { get; set; }

        /// <summary>
        /// 一本书的sql语句
        /// </summary>
        private string m_single_book_sql = string.Empty;


        #endregion

        #region 消息回送
        /// <summary>
        /// 发送消息到客户端
        /// </summary>
        /// <param name="i_msg"></param>
        private void SendMsg2Client(string i_msg, MsgType i_msg_type)
        {
            SendMsgEventArgs e = new SendMsgEventArgs();
            e.Msg = i_msg;
            e.MsgType = i_msg_type;
            if (SendMsg != null)
            {
                SendMsg(this, e);
            }

        }
        #endregion



        #region 收集书籍逻辑

        /// <summary>
        /// 收集小说详情
        /// </summary>
        public void StartCollectBook()
        {
            if (string.IsNullOrEmpty(ComName))
                return;


            BookDao t_book_dao = new BookDao();

            t_obj = ComManager.CreateBigSiteObj(ComName);

            string t_book_url = string.Empty;

            BookInfo t_book_info = null;

            Dictionary<string, string> t_book_dic = null;

            DataSet t_single_ds = null;

            long t_ret = 0;
            try
            {
                if (EndPageIndex != 1)
                {

                    for (int index = StartPageIndex; index < EndPageIndex; index++)
                    {
                        t_book_url = string.Format(BookUrl, index);

                        t_book_dic = t_obj.CollectBookList(t_book_url);

                        if (t_book_dic != null)
                        {
                            SendMsg2Client(string.Format("从地址：{0} 收录到{1}本小说", t_book_url, t_book_dic.Count), MsgType.Total);

                            foreach (string t_url in t_book_dic.Keys)
                            {
                                t_book_info = t_obj.CollectBookItem(t_url);

                                t_ret = t_book_dao.InsertBookInfo(t_book_info);

                                if (t_ret > 0)
                                {
                                    SendMsg2Client(string.Format("小说：{0} 成功入库", t_book_info.BookName), MsgType.Detail);

                                    //成功入库，下载小说封面及收录普通章节
                                    m_single_book_sql = string.Format("select bookId,bookName,chapterUrl,lastUpdateChapterName,bookCategoryId from tbbook where bookid={0}", t_ret);
                                    if (t_book_info.IsExistsImage && !string.IsNullOrEmpty(t_book_info.ImageUrl))
                                    {
                                        NetSiteCatchManager.DownLoadImage(t_book_info.ImageUrl, t_ret);
                                    }

                                    t_single_ds = t_book_dao.QueryData(m_single_book_sql);

                                    if (t_single_ds != null && t_single_ds.Tables[0].Rows.Count > 0)
                                    {
                                        CollectNormalChapter(t_single_ds.Tables[0].Rows[0]);
                                    }

                                    //收录评论
                                    if (!string.IsNullOrEmpty(t_book_info.CommentUrl))
                                    {
                                        List<string> t_comment_list = t_obj.CollectReview(t_book_info.CommentUrl);

                                        if (t_comment_list != null && t_comment_list.Count > 0)
                                        {
                                            SendMsg2Client(string.Format("bookId：{1} 收录评论：{0}条", t_comment_list.Count, t_ret), MsgType.Total);
                                            foreach (string t_comment in t_comment_list)
                                            {
                                                t_book_dao.InsertBookReview(t_ret, t_comment);
                                            }
                                        }
                                    }

                                }
                            }
                        }


                    }

                }
                else
                {
                    t_book_info = t_obj.CollectBookItem(BookUrl);

                    t_ret = t_book_dao.InsertBookInfo(t_book_info);

                    if (t_ret > 0)
                    {

                        SendMsg2Client(string.Format("小说：{0} 成功入库", t_book_info.BookName), MsgType.Detail);

                        //成功入库，下载小说封面及收录普通章节
                        m_single_book_sql = string.Format("select bookId,bookName,chapterUrl,lastUpdateChapterName,bookCategoryId from tbbook where bookid={0}", t_ret);
                        if (t_book_info.IsExistsImage && !string.IsNullOrEmpty(t_book_info.ImageUrl))
                        {
                            NetSiteCatchManager.DownLoadImage(t_book_info.ImageUrl, t_ret);
                        }

                        t_single_ds = t_book_dao.QueryData(m_single_book_sql);

                        if (t_single_ds != null && t_single_ds.Tables[0].Rows.Count > 0)
                        {
                            CollectNormalChapter(t_single_ds.Tables[0].Rows[0]);
                        }

                        //收录评论
                        if (!string.IsNullOrEmpty(t_book_info.CommentUrl))
                        {
                            List<string> t_comment_list = t_obj.CollectReview(t_book_info.CommentUrl);

                            if (t_comment_list != null && t_comment_list.Count > 0)
                            {
                                SendMsg2Client(string.Format("bookId：{1} 收录评论：{0}条", t_comment_list.Count, t_ret), MsgType.Total);
                                foreach (string t_comment in t_comment_list)
                                {

                                    t_book_dao.InsertBookReview(t_ret, t_comment);
                                }
                            }
                        }

                    }
                    else
                    {
                        SendMsg2Client(string.Format("小说：{0} 已经存在", t_book_info.BookName), MsgType.Detail);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("收录小说信息失败" + ex.ToString());
            }

        }

        #endregion

        #region 收集评论逻辑

        /// <summary>
        /// 开始收集评论
        /// </summary>
        public void StartCollectReview()
        {
            if (string.IsNullOrEmpty(BookSQL))
            {
                BookSQL = string.Format("select bookid,commentUrl from tbbook where isexistimage=0 and siteid={0}", SiteId);
            }
            t_obj = ComManager.CreateBigSiteObj(ComName);
            BookDao t_book_dao = new BookDao();
            long t_bookid = 0;
            try
            {
                DataSet ds = m_db_mgr.QueryData(BookSQL);

                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    SendMsg2Client(string.Format("需要收集评论的小说 {0} 本", ds.Tables[0].Rows.Count), MsgType.Total);

                    //收录评论
                    for (int index = 0; index < ds.Tables[0].Rows.Count; index++)
                    {
                        if (!string.IsNullOrEmpty(ds.Tables[0].Rows[index]["commentUrl"].ToString()))
                        {
                            List<string> t_comment_list = t_obj.CollectReview(ds.Tables[0].Rows[index]["commentUrl"].ToString());
                            if (!long.TryParse(ds.Tables[0].Rows[index]["bookid"].ToString(), out t_bookid))
                                t_bookid = 0;
                            if (t_comment_list != null && t_comment_list.Count > 0)
                            {
                                SendMsg2Client(string.Format("bookId：{1}收录评论：{0}条", t_comment_list.Count, t_bookid), MsgType.Total);
                                foreach (string t_comment in t_comment_list)
                                {

                                    t_book_dao.InsertBookReview(t_bookid, t_comment);
                                }
                            }
                        }
                    }

                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("收集评论详情失败" + ex.ToString());
            }
        }



        #endregion



        #region 普通章节收录逻辑

        /// <summary>
        /// 开始普通章节收录
        /// </summary>
        public void StartNormalChapter()
        {
            string t_str_sql = "select bookId,chapterUrl,lastUpdateChapterName,bookCategoryId from tbbook a where not exists(select 1 from tbbookchapter b where a.bookid=b.bookid)";
            try
            {
                DataSet ds = m_db_mgr.QueryData(t_str_sql);
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    SendMsg2Client(string.Format("需要收录 {0} 本小说普通章节", ds.Tables[0].Rows.Count), MsgType.Total);

                    for (int index = 0; index < ds.Tables[0].Rows.Count; index++)
                    {
                        CollectNormalChapter(ds.Tables[0].Rows[index]);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("收集vip书籍的普通章节失败" + ex.ToString());
            }
        }

        private void CollectNormalChapter(DataRow i_book_row)
        {
            t_obj = ComManager.CreateBigSiteObj(ComName);


            try
            {
                List<BookChapterInfo> t_chapter_list_source = t_obj.CollectChapterList(i_book_row["chapterUrl"].ToString());
                string t_last_update_chapter_name = i_book_row["lastUpdateChapterName"] == null ? "" : i_book_row["lastUpdateChapterName"].ToString();

                int t_chapter_index = 0;

                int t_chapter_count = 0;

                string t_chapter_content = string.Empty;

                if (t_chapter_list_source != null && t_chapter_list_source.Count > 0)
                {
                    SendMsg2Client(string.Format("地址：{0} 收集到{1}个章节..", i_book_row["chapterUrl"].ToString(), t_chapter_list_source.Count), MsgType.Total);

                    if (!string.IsNullOrEmpty(t_last_update_chapter_name))
                    {
                        t_chapter_index = t_chapter_list_source.FindIndex(delegate(BookChapterInfo t_chapter_info)
                        {
                            return t_chapter_info.ChapterName == t_last_update_chapter_name;
                        });
                    }
                }
                if (t_chapter_index < 0)
                    t_chapter_index = 0;

                long t_book_id = 0;
                long t_chapter_id = 0;


                if (!long.TryParse(i_book_row["bookId"].ToString(), out t_book_id))
                    t_book_id = 0;

                BookChapterInfo t_chapter = null;

                List<BookChapterInfo> t_vip_chapter_list = t_chapter_list_source.FindAll(delegate(BookChapterInfo d_chapter)
                {
                    return d_chapter.IsVip == true;
                });

                for (int index = t_chapter_index; index < t_chapter_list_source.Count; index++)
                {
                    t_chapter = t_chapter_list_source[index];


                    t_chapter.BookId = t_book_id;
                    t_chapter.SiteId = (int)Consts.Site.qidian;

                    if (t_chapter.IsVip == false)
                    {
                        t_chapter_content = t_obj.CollectChapterContent(t_chapter_list_source[index].Comfrom);

                        t_chapter_count++;

                        //如果章节为空
                        if (string.IsNullOrEmpty(t_chapter_content))
                        {
                            t_chapter_content = t_obj.CollectChapterContent(t_chapter_list_source[index].Comfrom);

                            t_chapter_count++;

                        }

                        //章节入库
                        t_chapter.WordsCount = t_chapter_content.Length;
                        //章节信息保存到数据库
                        t_chapter_id = m_db_mgr.InsertBookChapter(t_chapter, t_chapter.ChapterName);

                        //章节内容保存到磁盘
                        if (t_chapter_id > 0)
                        {
                            SendMsg2Client(string.Format("收集到章节 ：{0}", t_chapter.ChapterName), MsgType.Detail);

                            m_file_mgr.WriteBookContent(i_book_row["bookCategoryId"].ToString(), t_book_id, t_chapter_id, t_chapter_content);

                        }

                    }
                }

                //收录vip章节
                if (t_vip_chapter_list != null && t_vip_chapter_list.Count > 0)
                {
                    SmallSiteObject t_google_mgr = new SmallSiteObject();

                    t_vip_chapter_list = t_google_mgr.CollectBookChapterList(i_book_row["bookName"].ToString(), t_vip_chapter_list);

                    if (t_vip_chapter_list != null && t_vip_chapter_list.Count > 0)
                    {
                        foreach (BookChapterInfo t_vip in t_vip_chapter_list)
                        {
                            t_vip.BookId = t_book_id;

                            t_vip.SiteId = SiteId;
                            //章节信息保存到数据库
                            t_chapter_id = m_db_mgr.InsertBookChapter(t_vip, t_vip.ChapterName);

                            //章节内容保存到磁盘
                            if (t_chapter_id > 0)
                            {
                                SendMsg2Client(string.Format("收集到VIP章节 ：{0}", t_vip.ChapterName), MsgType.Detail);

                                m_file_mgr.WriteBookContent(i_book_row["bookCategoryId"].ToString(), t_book_id, t_chapter_id, t_vip.ChapterContent);

                            }
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("收集普通章节失败" + ex.ToString());
            }
        }
        #endregion

        #region 下载小说封面
        /// <summary>
        /// 收集封面
        /// </summary>
        public void StartDownLoadImage()
        {
            SendMsg2Client("开始收集封面...", MsgType.Total);

            string t_str_sql = "select bookId,imageUrl from tbbook where isexistimage=0";

            try
            {

                DataSet ds = m_db_mgr.QueryData(t_str_sql);

                long t_book_id = 0;

                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    SendMsg2Client(string.Format("需要下载{0}本小说封面...", ds.Tables[0].Rows.Count), MsgType.Total);
                    for (int index = 0; index < ds.Tables[0].Rows.Count; index++)
                    {
                        if (ds.Tables[0].Rows[index]["imageUrl"] != DBNull.Value && !string.IsNullOrEmpty(ds.Tables[0].Rows[index]["imageUrl"].ToString()))
                        {
                            if (!long.TryParse(ds.Tables[0].Rows[index]["bookId"].ToString(), out t_book_id))
                                t_book_id = 0;



                            int t_ret = NetSiteCatchManager.DownLoadImage(ds.Tables[0].Rows[index]["imageUrl"].ToString(), t_book_id);
                            if (t_ret == Consts.Success)
                            {
                                SendMsg2Client(string.Format("成功下载：{0}封面", t_book_id), MsgType.Total);
                                BookDao t_book_dao = new BookDao();
                                t_book_dao.UpdateBookExistImage(t_book_id, 1);

                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("获取封面失败" + ex.ToString());
            }
        }
        #endregion


        #region 百度收录逻辑

        /// <summary>
        /// 百度收录章节内容逻辑
        /// </summary>
        public void StartBaiDuSearchCahapter()
        {
            try
            {
                if (string.IsNullOrEmpty(BookSQL))
                {
                    BookSQL = "select bookCategoryId, bookid,bookName,chapterUrl,lastupdatechaptername from tbbook a where  not exists(select 1 from tbbookcompleted b where a.bookid=b.bookid)";
                }

                m_baidu_ds = m_db_mgr.QueryData(BookSQL);

                //创建接口对象
                if (m_baidu_ds.Tables[0].Rows[0]["chapterUrl"].ToString().IndexOf("www.qidian.com") > -1)
                {
                    t_obj = ComManager.CreateBigSiteObj("www.qidian.com");
                }

                if (m_baidu_ds != null && m_baidu_ds.Tables[0].Rows.Count > 0)
                {
                    m_total_count = m_baidu_ds.Tables[0].Rows.Count;

                    m_per_thread_count = m_total_count / ThreadCount;

                    for (int index = 0; index < ThreadCount; index++)
                    {
                        Thread t_thd = new Thread(ThreadBaiduCollect);

                        t_thd.Start(index);
                    }

                    for (int index = ThreadCount * m_per_thread_count; index < m_total_count; index++)
                    {
                        GetChapterContentFromBaiDu(m_baidu_ds.Tables[0].Rows[index]);
                    }

                    SendMsg2Client(string.Format("{0}本小说需要收录章节，{1}个线程同时进行，每个线程{2}本书", m_total_count, ThreadCount, m_per_thread_count), MsgType.Total);
                }



            }
            catch (Exception ex)
            {
                if (ex.ToString().IndexOf("线程") > -1)
                {
                    SendMsg2Client("已经停止!", MsgType.Total);
                }
                else
                {
                    LogHelper.Error("收集小说章节失败" + ex.ToString());
                }
            }
        }

        /// <summary>
        /// 收集小说章节
        /// </summary>
        /// <param name="i_thd_index"></param>
        private void ThreadBaiduCollect(object i_thd_index)
        {
            int t_thd_index = (int)i_thd_index;

            for (int index = t_thd_index * m_per_thread_count; index < (t_thd_index + 1) * m_per_thread_count; index++)
            {
                GetChapterContentFromBaiDu(m_baidu_ds.Tables[0].Rows[index]);
            }

        }

        private void GetChapterContentFromBaiDu(DataRow i_book_row)
        {

            try
            {
                if (t_obj == null && i_book_row["chapterUrl"].ToString().IndexOf("www.qidian.com") > -1)
                {
                    t_obj = ComManager.CreateBigSiteObj("www.qidian.com");
                }

                List<BookChapterInfo> t_chapter_list_source = t_obj.CollectChapterList(i_book_row["chapterUrl"].ToString());


                string t_last_update_chapter_name = i_book_row["lastUpdateChapterName"] == null ? "" : i_book_row["lastUpdateChapterName"].ToString();

                int t_chapter_index = 0;

                int t_vip_chapter_count = 0;
                int t_chapter_count = 0;

                string t_chapter_content = string.Empty;

                bool t_is_compared = false;

                if (t_chapter_list_source != null && t_chapter_list_source.Count > 0)
                {
                    SendMsg2Client(string.Format("小说：{2} 从地址：{0} 收集到{1}个章节..", i_book_row["chapterUrl"].ToString(), t_chapter_list_source.Count, i_book_row["bookName"]), MsgType.Total);

                    if (!string.IsNullOrEmpty(t_last_update_chapter_name))
                    {
                        t_chapter_index = t_chapter_list_source.FindIndex(delegate(BookChapterInfo t_chapter_info)
                        {
                            t_is_compared = NetSiteCatchManager.CompareChapterName(t_chapter_info.ChapterName, t_last_update_chapter_name);
                            if (t_is_compared == false)
                            {
                                t_is_compared = NetSiteCatchManager.CompareChapterName(t_last_update_chapter_name, t_chapter_info.ChapterName);
                            }
                            return t_is_compared;
                        });
                    }
                }
                if (t_chapter_index < 0)
                    t_chapter_index = 0;

                long t_book_id = 0;
                long t_chapter_id = 0;
                string t_chapter_url = string.Empty;

                if (!long.TryParse(i_book_row["bookId"].ToString(), out t_book_id))
                    t_book_id = 0;

                BookChapterInfo t_chapter = null;

                for (int index = t_chapter_index; index < t_chapter_list_source.Count; index++)
                {
                    t_chapter = t_chapter_list_source[index];

                    t_chapter.BookId = t_book_id;
                    t_chapter.SiteId = (int)Consts.Site.qidian;

                    if (t_chapter.IsVip == false)
                    {
                        t_chapter_content = t_obj.CollectChapterContent(t_chapter_list_source[index].Comfrom);
                        t_chapter_content = NetSiteCatchManager.ReplaceContent(t_chapter_content);
                        t_chapter_count++;
                        //如果章节为空
                        if (string.IsNullOrEmpty(t_chapter_content))
                        {
                            t_chapter_content = GetChapterContentFromBaiDu(i_book_row["bookName"].ToString(), t_chapter.ChapterName, out t_chapter_url);

                        }

                    }
                    else
                    {
                        t_chapter_content = GetChapterContentFromBaiDu(i_book_row["bookName"].ToString(), t_chapter.ChapterName, out t_chapter_url);

                    }
                    if (!string.IsNullOrEmpty(t_chapter_content) && t_chapter_content.Length > 2000)
                    {
                        if (t_chapter.IsVip)
                        {
                            if (!Regex.IsMatch(t_chapter_content, "[\u4E00-\u9FA5]", RegexOptions.IgnoreCase | RegexOptions.Compiled))
                            {
                                break;
                            }
                            t_vip_chapter_count++;
                        }
                        //章节入库
                        t_chapter.WordsCount = t_chapter_content.Length;
                        t_chapter.Comfrom = t_chapter_url;
                        //章节信息保存到数据库
                        t_chapter_id = m_db_mgr.InsertBookChapter(t_chapter, t_chapter.ChapterName);

                        //章节内容保存到磁盘
                        if (t_chapter_id > 0)
                        {
                            m_file_mgr.WriteBookContent(i_book_row["bookCategoryId"].ToString(), t_book_id, t_chapter_id, t_chapter_content);
                            SendMsg2Client(string.Format("从地址：{0} 收集 {1} 章节内容,章节内容字数：{2}", t_chapter.Comfrom, t_chapter.ChapterName, t_chapter_content.Length), MsgType.Detail);
                        }
                    }
                    else
                    {
                        break;
                    }
                }

                SendMsg2Client(string.Format("小说：{2} 收集到 {0} 个VIP章节,{1} 个普通章节", t_vip_chapter_count, t_chapter_count, i_book_row["bookName"]), MsgType.Total);





            }
            catch (Exception ex)
            {
                LogHelper.Error("获取网站代码失败" + ex.ToString());
            }

        }

        private string GetChapterContentFromBaiDu(string i_book_name, string i_chapter_name, out string o_chapter_url)
        {
            //百度首页编码
            Encoding t_encoding = Encoding.GetEncoding("gb2312");

            o_chapter_url = string.Empty;

            string t_chapter_content_url = string.Format("http://www.baidu.com/s?wd={0}", HttpUtility.UrlEncode(i_chapter_name, t_encoding));

            string t_chapter_content = string.Empty;

            string t_html = NetSiteCatchManager.ReadUrl(t_chapter_content_url, t_encoding);

            string t_url = string.Empty;

            if (!string.IsNullOrEmpty(t_html))
            {
                MatchCollection t_ma = Regex.Matches(t_html, "<h3\\s*?class=[\'\"]?t[\'\"]?><a[^<>]*?hrefs*=s*[\'\"]*([^\"\']*)[\'\"]*[^<>]*?>(.*?)</a>\\s*?</h3>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                if (t_ma == null || t_ma.Count <= 0)
                    return string.Empty;

                foreach (Match t_mc in t_ma)
                {
                    if (t_mc.Groups.Count == 3)
                    {
                        t_url = t_mc.Groups[1].Value.ToString();

                        if(!NetSiteCatchManager.IsPiraticSite(t_url))
                            continue;

                        if (string.IsNullOrEmpty(t_chapter_content))
                        {
                            t_chapter_content = GetContent(t_url, i_book_name, i_chapter_name);
                            if (!string.IsNullOrEmpty(t_chapter_content))
                            {
                                //过滤章节前面导航之类的信息
                                int t_index = i_chapter_name.IndexOf(" ");
                                if (t_index > -1)
                                {
                                    i_chapter_name = i_chapter_name.Substring(0, t_index);
                                }
                                t_index = t_chapter_content.IndexOf(i_chapter_name);
                                if (t_index > -1)
                                {
                                    t_chapter_content = t_chapter_content.Substring(t_index);
                                }
                                t_chapter_content = NetSiteCatchManager.ReplaceContent(t_chapter_content);
                            }
                            if (string.IsNullOrEmpty(t_chapter_content) || t_chapter_content.Length < 1500)
                                continue;
                            else
                                break;
                        }
                        else
                        {

                            break;
                        }
                    }
                }

                t_chapter_content = string.Format("document.write('{0}');", t_chapter_content);
                o_chapter_url = t_url;
                return t_chapter_content;
            }

            return string.Empty;
        }

        private string GetContent(string i_url, string i_book_name, string i_chapter_name)
        {

            string t_chapter_content = "";
            string t_chapter_temp_content = "";
            Encoding t_encoding = Encoding.Default;
            string t_charset = string.Empty;
            string t_html = string.Empty;
            Dictionary<string, string> t_dic = new Dictionary<string, string>();

            try
            {
                t_html = NetSiteCatchManager.ReadUrl(i_url, t_encoding);

                /*
                if (!string.IsNullOrEmpty(t_html))
                {
                    //不包含章节名称
                    if (!IsContainChapterName(i_book_name, i_chapter_name, t_html))
                    {
                        Match t_mc_charset = Regex.Match(t_html, "<meta[^<>]*?charsets*=s*[\'\"]*([^\"\'\\s\\/]*)[\'\"]*[^<>]*?>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                        if (t_mc_charset != null)
                        {
                            t_charset = t_mc_charset.Groups[1].Value.ToString();

                            t_encoding = Encoding.GetEncoding(t_charset);

                            t_html = NetSiteCatchManager.ReadUrl(i_url, t_encoding);

                            if (!IsContainChapterName(i_book_name, i_chapter_name, t_html))
                                return string.Empty;
                        }
                    }
                }*/

                if (!NetSiteCatchManager.IsContainChapterName(i_book_name, i_chapter_name, t_html))
                    return string.Empty;

                if (!string.IsNullOrEmpty(t_html))
                {
                    HtmlDocument t_doc = HtmlDocument.Create(t_html);

                    HtmlNodeCollection t_node_collect = t_doc.GetElementsByTagName("div");

                    HtmlElement t_ele = null;

                    if (t_node_collect != null && t_node_collect.Count > 0)
                    {
                        for (int index = t_node_collect.Count - 1; index > 0; index--)
                        {
                            t_ele = (HtmlElement)t_node_collect[index];
                            t_chapter_temp_content = t_ele.InnerText;
                            t_chapter_temp_content = Regex.Replace(t_chapter_temp_content, "[a-zA-Z0-9;]", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                            if (t_chapter_temp_content.Length > t_chapter_content.Length && t_chapter_temp_content.Length > 2000)
                            {
                                t_chapter_content = t_chapter_temp_content;
                                t_dic.Add(t_chapter_content, t_ele.HTML);
                            }
                        }
                    }

                    if (t_chapter_content.Length > 2000)
                    {
                        return t_dic[t_chapter_content];
                    }

                    t_node_collect = t_doc.GetElementsByTagName("td");

                    if (t_node_collect != null && t_node_collect.Count > 0)
                    {
                        for (int index = t_node_collect.Count - 1; index > 0; index--)
                        {
                            t_ele = (HtmlElement)t_node_collect[index];
                            t_chapter_temp_content = t_ele.InnerText;
                            t_chapter_temp_content = Regex.Replace(t_chapter_temp_content, "[a-zA-Z0-9;]", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                            if (t_chapter_temp_content.Length > t_chapter_content.Length && t_chapter_temp_content.Length > 2000)
                            {
                                t_chapter_content = t_chapter_temp_content;
                                t_dic.Add(t_chapter_content, t_ele.HTML);
                            }
                        }
                    }

                    if (t_chapter_content.Length > 2000)
                        return t_dic[t_chapter_content];
                }

                return string.Empty;

            }
            catch (Exception ex)
            {
                LogHelper.Error("从搜索引擎获取页面内容失败" + ex.ToString());
                return string.Empty;
            }
        }
        #endregion
    }
        
}
